<?php

namespace app\screen\controller;

use app\admin\validate\User;
use app\common\business\ScreenUser;
use app\common\lib\Param;
use app\common\lib\Show;
use think\response\Json;

class Login extends ScreenBase
{
    /**
     * 用户登录
     * @return Json
     */
    public function login(): Json
    {
        list($username, $password,$code,$key) = Param::postMore([
            ['username', ''],
            ['password', ''],
            ['code', ''],
            ['key', ''],
        ], $this->request, true);
        $data = [
            'username' => $username,
            'password' => $password,
            'code'=>$code,
            'key'=>$key
        ];
        $validate = new User();
        if (!$validate->scene('login')->check($data)) {
            return Show::error([], $validate->getError());
        }
        try {
            $result = (new ScreenUser())->login($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success($result, "登录成功");
        }
        return Show::error([], "登录失败");
    }
}
